define(function(require, exports, module){
    require('basic');
    require('highstock');
    var config = require('config');
    var user = require('user');
    var model = function(){
        this.init();
        //console.log(config.login)
    };
    model.prototype = {
        init: function(){
            var sf = this;
            this.refresh();
        },
        refresh: function(){
            this.analysis_progress();
            this.analysis_top();
            this.analysis_potusage();
            this.intranet_list();
            this.extranet_list();
            this.temp();
        },
        /**
         * 获取项目状态分配饼图数据
         * @return {[type]} [description]
         */
        analysis_progress: function(){
            var sf = this;
            $.ajax({
                type: "GET",
                url: config.analysis_progress+user.info.leadPost,
                success: function(data){
                    var obj = {
                        title: {
                            text: '项目分配状态'
                        },
                        series: [{
                            name: '数量',
                            type: 'pie',
                            data: [
                                ['待执行项目', data.tobe],
                                ['已执行项目', data.done],
                                ['执行中项目', data.doing]
                            ]
                        }]
                    }
                    $('#chart1').highcharts(obj);
                    setTimeout(function(){
                        sf.analysis_progress();
                    },30000);
                } 
            });
        },
        /**
         * Top5 实例数最多的项目  
         * @return {[type]} [description]
         */
        analysis_top: function(){
            var sf = this;
            $.ajax({
                type: "GET",
                url: config.analysis_top+user.info.leadPost+'&number=5',
                success: function(data){
                    var name = [];
                    var value = []
                    for(var i in data){
                        name.push(data[i]['project_name']);
                        value.push(data[i]['count']);
                    }
                    var obj = {
                        chart: {
                            type: 'bar'
                        },
                        title: {
                            text: 'Top5 实例数最多项目'
                        },
                        xAxis: {
                            categories: name
                        },
                        series: [{
                            name: '数量',
                            data: value
                        }],
                        plotOptions: {
                            bar: {
                                dataLabels: {
                                    enabled: true
                                }
                            }
                        }
                    };

                    $('#chart2').highcharts(obj);
                    setTimeout(function(){
                        sf.analysis_top();
                    },30000);
                } 
            });
        },
        
        analysis_potusage: function(){
            var sf = this;
            $.ajax({
                type: "GET",
                url: config.pool_list+user.info.leadPost,
                success: function(data){
                    if (data.length > 1) {
                        $('#pt0804').show();
                    }
                    var num = 0;
                    $(data).each(function(index,item){
                        var poolName = item.name;
                        $('#f4h4 h4').eq(index).text('主机池' + poolName + '使用统计');
                        $.ajax({
                            type: "GET",
                            url: config.pool+'/'+item.pool_id+'/list'+user.info.leadPost,
                            success: function(data2){
                                /* console.log(data2);
                                var ca_arr = [],obj = {};

                                for (var i = 0;i<data2.length; i++){

                                    //console.log(obj['运行'+data[i]['cacount']+'C的POT数量']);
                                    if(obj['运行'+data2[i]['cacount']+'个容器的主机数量']){
                                        //2. 当再次遇到cacount=0就来if这里++
                                        obj['运行'+data2[i]['cacount']+'个容器的主机数量']++;
                                    }else{
                                        //1. 以cacount=0为例，第一次遇到cacount=0时，if里的判断总是undefined, 所以总是先来else创建，并设为值等于1
                                        obj['运行'+data2[i]['cacount']+'个容器的主机数量'] = 1;
                                    }
                                    //console.log(obj['运行'+data[i]['cacount']+'C的POT数量']);
                                    //console.log(obj);
                                }
                                //循环obj, 创建最终的数组
                                for(var o in obj){
                                    ca_arr.push([o,obj[o]]);
                                }

                                var obj3 = {
                                    title: {
                                        text: poolName
                                    },
                                    series: [{
                                        name: '数量',
                                        type: 'pie',
                                        data: ca_arr
                                    }]
                                };
                                $('#pt0629 .ct').eq(index).highcharts(obj3);*/

                                var sub_data_obj = {};
                                for (var i = 0; i < data2.length; i++) {
                                    var t1 = data2[i],
                                        obj= {};
                                    obj['type'] = t1['type'];
                                    obj['cacount'] = t1['cacount'];
                                    obj['name'] = t1['name'];

                                    sub_data_obj[t1['type']] = sub_data_obj[t1['type']] ? sub_data_obj[t1['type']] : [];
                                    sub_data_obj[t1['type']].push(obj);
                                }
                                //console.log(sub_data_obj);
                                
                                for (var j in sub_data_obj){
                                    //console.log(j)
                                    var ca_arr = [],obj2 = {};
                                    for (var k = 0; k<sub_data_obj[j].length; k++){
                                        //console.log(sub_data_obj[j][k]['cacount']);
                                        if(obj2['运行'+sub_data_obj[j][k]['cacount']+'个容器的主机数量']){
                                            obj2['运行'+sub_data_obj[j][k]['cacount']+'个容器的主机数量']++;
                                        }else{
                                            obj2['运行'+sub_data_obj[j][k]['cacount']+'个容器的主机数量'] = 1;
                                        }
                                    }
                                    for(var o in obj2){
                                        ca_arr.push([o,obj2[o]]);
                                    }
                                    //console.log(ca_arr);

                                    var obj3 = {
                                        title: {
                                            text: j
                                        },
                                        plotOptions: {
                                            pie: {
                                                allowPointSelect: true,
                                                cursor: 'pointer',
                                                dataLabels: {
                                                    enabled: false
                                                },
                                                showInLegend: true
                                            }
                                        },
                                        series: [{
                                            name: '数量',
                                            type: 'pie',
                                            data: ca_arr
                                        }]
                                    };

                                    $('#f4h4 .ct').eq(num).highcharts(obj3);
                                    num++;
                                    console.log(num);

                                }                                

                                setTimeout(function(){
                                    sf.analysis_potusage();
                                },30000);
                            }
                        });
                    });
                }
            });
            /**
             * 项目分配状态，只有已分配未分配， 用到数据 alloced
             * POT运行状态，只有已分配未分配， 用到数据 runing
             * @return {[type]} [description]
             */
            /*$.ajax({
                type: "GET",
                url: config.analysis_potusage+user.info.leadPost,
                success: function(data){
                    //console.log(data);
                    var t = 0, f = 0, rt = 0, rf = 0;
                    var ca0=ca1=ca2=ca3=ca4=ca5=ca6=ca7=ca8=ca16=0;
                    var ca_arr = [],obj = {};
                    for (var i = 0;i<data.length; i++){
                        //以下统计 alloced[项目分配状态]的数量
                        if(data[i].alloced == true) t++;
                        if(data[i].alloced == false) f++;
                        //以下统计 runing[POT运行状态]的数量
                        if(data[i].runing == true) rt++;
                        if(data[i].runing == false) rf++;


                        //console.log(obj['运行'+data[i]['cacount']+'C的POT数量']);
                        if(obj['运行'+data[i]['cacount']+'C的POT数量']){
                            //2. 当再次遇到cacount=0就来if这里++
                            obj['运行'+data[i]['cacount']+'C的POT数量']++;
                        }else{
                            //1. 以cacount=0为例，第一次遇到cacount=0时，if里的判断总是undefined, 所以总是先来else创建，并设为值等于1
                            obj['运行'+data[i]['cacount']+'C的POT数量'] = 1;
                        }
                        //console.log(obj['运行'+data[i]['cacount']+'C的POT数量']);
                        //console.log(obj);

                    }
                    //循环obj, 创建最终的数组
                    for(var o in obj){
                        ca_arr.push([o,obj[o]]);
                    }



                    //console.log(ca_arr);
                    var obj1 = {
                        title: {
                            text: 'POT池使用统计'
                        },
                        series: [{
                            type: 'pie',
                            data: [
                                ['已分配POT', t],
                                ['未分配POT', f]
                            ]
                        }]
                    };
                    var obj2 = {
                        title: {
                            text: 'POT运行状态'
                        },
                        series: [{
                            type: 'pie',
                            data: [
                                ['已分配POT', rt],
                                ['未分配POT', rf]
                            ]
                        }]
                    };
                    var obj3 = {
                        title: {
                            text: 'POT容器使用率'
                        },
                        series: [{
                            type: 'pie',
                            data: ca_arr
                        }]
                    };
                    $('#chart3').highcharts(obj1);
                    $('#chart7').highcharts(obj2);
                    $('#chart4').highcharts(obj3);

                } 
            });*/
        },

        /**
         * 内网规则，同样用 alloced 判断已分配未分配
         * @return {[type]} [description]
         */
        intranet_list: function(){
            var sf = this;
            $.ajax({
                type: "GET",
                url: config.intranet_list+user.info.leadPost,
                success: function(data){
                    var t = 0, f = 0;
                    for (var i = 0;i<data.length; i++){
                        //以下统计 alloced[分配状态]的数量
                        if(data[i].alloced == true) t++;
                        if(data[i].alloced == false) f++;
                    }
                    var obj1 = {
                        title: {
                            text: '内网地址'
                        },
                        series: [{
                            name: '数量',
                            type: 'pie',
                            data: [
                                ['已分配', t],
                                ['未分配', f]
                            ]
                        }]
                    };
                    $('#chart5').highcharts(obj1);
                    setTimeout(function(){
                        sf.intranet_list();
                    },30000);
                } 
            });
        },

        /**
         * 外网规则，同样用 alloced 判断已分配未分配
         * @return {[type]} [description]
         */
        extranet_list: function(){
            var sf = this;
            $.ajax({
                type: "GET",
                url: config.extranet_list+user.info.leadPost,
                success: function(data){
                    var t = 0, f = 0;
                    for (var i = 0;i<data.length; i++){
                        //以下统计 alloced[分配状态]的数量
                        if(data[i].alloced == true) t++;
                        if(data[i].alloced == false) f++;
                    }
                    var obj1 = {
                        title: {
                            text: '外网地址'
                        },
                        series: [{
                            name: '数量',
                            type: 'pie',
                            data: [
                                ['已分配', t],
                                ['未分配', f]
                            ]
                        }]
                    };
                    $('#chart6').highcharts(obj1);
                    setTimeout(function(){
                        sf.extranet_list();
                    },30000);
                } 
            });
        },



        temp: function(){







    /*$('#chart8').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: '服务实例统计'
        },
        xAxis: {
            categories: ['Apache', 'Nginx', 'Tomcat', 'Redis', 'Couchbase', 'InnoSQL 5.5', 'MySQL 5.6']
        },
        yAxis: {
            min: 0,
            title: {
                text: ''
            },
            stackLabels: {
                enabled: true,
                style: {
                    fontWeight: 'bold',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                }
            }
        },
        plotOptions: {
            column: {
                stacking: 'normal',
                dataLabels: {
                    enabled: false,
                    color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
                }
            }
        },
        series: [{
            name: '停止',
            data: [5, 3, 4, 7, 2, 4, 3]
        }, {
            name: '运行',
            data: [2, 2, 3, 2, 1,3 ,5]
        }]
    });*/



        }
    };

    module.exports = new model();
});







/*$(function() {
    $('#chart1').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '项目分配状态'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false
                },
                showInLegend: true
            }
        },
        series: [{
            type: 'pie',
            data: [
                ['待执行项目', 30.0],
                ['已执行项目', 30.0],
                ['执行中项目', 40.0]
            ]
        }]
    });

    $('#chart2').highcharts({
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Top5 实例数最多项目'
        },
        xAxis: {
            categories: ['银保通', '在线理财', '微信银行', '银证转账', '电子票据']
        },
        yAxis: {
            min: 0,
            title: {
                align: 'high'
            },
            labels: {
                overflow: 'justify'
            }
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -40,
            y: 100,
            floating: true,
            borderWidth: 1,
            backgroundColor: '#FFFFFF',
            shadow: true
        },
        credits: {
            enabled: false
        },
        series: [{
            data: [107, 131, 235, 303, 456]
        }]
    });
    $('#chart3').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'POT池使用统计'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false
                },
                showInLegend: true
            }
        },
        series: [{
            type: 'pie',
            data: [
                ['已分配POT', 60.0],
                ['未分配POT', 40.0]
            ]
        }]
    });
    $('#chart4').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'POT容器使用率'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false
                },
                showInLegend: true
            }
        },
        series: [{
            type: 'pie',
            data: [
                ['运行8C的POT占比', 20.0],
                ['运行4C的POT占比', 20.0],
                ['运行2C的POT占比', 30.0],
                ['运行1C的POT占比', 30.0]
            ]
        }]
    });
    $('#chart5').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '外网地址'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false
                },
                showInLegend: true
            }
        },
        series: [{
            type: 'pie',
            data: [
                ['已分配', 60.0],
                ['未分配', 40.0]
            ]
        }]
    });
    $('#chart6').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '内网地址'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false
                },
                showInLegend: true
            }
        },
        series: [{
            type: 'pie',
            data: [
                ['已分配', 60.0],
                ['未分配', 40.0]
            ]
        }]
    });
    $('#chart7').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'POT运行状态'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false
                },
                showInLegend: true
            }
        },
        series: [{
            type: 'pie',
            data: [
                ['已分配POT', 60.0],
                ['未分配POT', 40.0]
            ]
        }]
    });
    $('#chart8').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: '服务实例统计'
        },
        xAxis: {
            categories: ['Apache', 'Nginx', 'Tomcat', 'Redis', 'Couchbase', 'InnoSQL 5.5', 'MySQL 5.6']
        },
        yAxis: {
            min: 0,
            title: {
                text: ''
            },
            stackLabels: {
                enabled: true,
                style: {
                    fontWeight: 'bold',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                }
            }
        },
        plotOptions: {
            column: {
                stacking: 'normal',
                dataLabels: {
                    enabled: false,
                    color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
                }
            }
        },
        series: [{
            name: '停止',
            data: [5, 3, 4, 7, 2, 4, 3]
        }, {
            name: '运行',
            data: [2, 2, 3, 2, 1,3 ,5]
        }]
    });
});*/
